package com.styytech.dadabao.ui.my;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.constant.ConstantsCommon;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.ui.more.NoticeListFragment;

import java.util.List;

/**
 * 服务公告
 */
@ContentView(R.layout.activity_message_list)
public class MessageFragmentActivity extends BaseActivity {
    /*全部*/
    @ViewInject(R.id.tv_all)
    TextView tv_all;
    /*全部*/
    @ViewInject(R.id.v_all)
    View v_all;
    /*未读*/
    @ViewInject(R.id.tv_unread)
    TextView tv_unread;
    /*未读*/
    @ViewInject(R.id.v_unread)
    View v_unread;
    /*已读*/
    @ViewInject(R.id.tv_read)
    TextView tv_read;
    /*已读*/
    @ViewInject(R.id.v_read)
    View v_read;

    /*上下文*/
    private Context mContext;
    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    /*服务公告列表*/
    private NoticeListFragment noticeListFragmentAll = null;
    private NoticeListFragment noticeListFragmentUnread = null;
    private NoticeListFragment noticeListFragmentRead = null;

    private boolean readStatus = false;// false:列表不自动刷新，true:列表自动刷新

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    /**
     * 初始数据
     */
    private void initData() {
        mContext = this;
        fragmentManager = getSupportFragmentManager();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setTopBar("站内通知", View.VISIBLE);
        initFragment();
    }

    /**
     * 初始化fragment
     */
    private void initFragment() {

        transaction = fragmentManager.beginTransaction();
        String newsUrl = ConstantsServerUrl.getMessageListUrl();
        noticeListFragmentAll = NoticeListFragment.newInstance(newsUrl,
                ConstantsCommon.DATA_SRC_MESSAGE,
                ConstantsCommon.ADAPTER_TYPE_MESSAGE, -1);
        transaction.replace(R.id.fl_content, noticeListFragmentAll, "ID_MESSAGE_ALL");
        transaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(readStatus && noticeListFragmentUnread != null){
            noticeListFragmentUnread.refreshData();
        }
    }

    @OnClick({R.id.tv_all, R.id.tv_unread, R.id.tv_read})
    public void OnClick(View v) {
        transaction = fragmentManager.beginTransaction();
        String newsUrl = null;
        switch (v.getId()) {
            case R.id.tv_all:
                readStatus = false;
                nomalStyle(R.id.tv_all);
                newsUrl = ConstantsServerUrl.getMessageListUrl();
                noticeListFragmentAll = NoticeListFragment.newInstance(newsUrl,
                        ConstantsCommon.DATA_SRC_MESSAGE,
                        ConstantsCommon.ADAPTER_TYPE_MESSAGE, -1);
                transaction.replace(R.id.fl_content, noticeListFragmentAll, "ID_MESSAGE_ALL");
                break;
            case R.id.tv_unread:
                readStatus = true;
                nomalStyle(R.id.tv_unread);
                newsUrl = ConstantsServerUrl.getMessageListUrl();
                noticeListFragmentUnread = NoticeListFragment.newInstance(newsUrl,
                        ConstantsCommon.DATA_SRC_MESSAGE,
                        ConstantsCommon.ADAPTER_TYPE_MESSAGE, 0);
                transaction.replace(R.id.fl_content, noticeListFragmentUnread, "ID_MESSAGE_UNREAD");
                break;
            case R.id.tv_read:
                readStatus = false;
                nomalStyle(R.id.tv_read);
                newsUrl = ConstantsServerUrl.getMessageListUrl();
                noticeListFragmentRead = NoticeListFragment.newInstance(newsUrl,
                        ConstantsCommon.DATA_SRC_MESSAGE,
                        ConstantsCommon.ADAPTER_TYPE_MESSAGE, 1);
                transaction.replace(R.id.fl_content, noticeListFragmentRead, "ID_MESSAGE_READ");
                break;
        }
        transaction.commit();
    }

    /**
     * 提示消息
     *
     * @param str 需要提示的消息
     */
    private void toast(String str) {
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        List<Fragment> fList = fragmentManager.getFragments();
        if (fList == null) {
            return;
        }
        for (Fragment fragment : fList) {
            if (fragment != null) {
                transaction.hide(fragment);
            }
        }
    }

    private void nomalStyle(int id) {
//        hideFragments(transaction);
        tv_all.setTextColor(getResources().getColor(R.color.black3));
        tv_unread.setTextColor(getResources().getColor(R.color.black3));
        tv_read.setTextColor(getResources().getColor(R.color.black3));
        v_all.setBackgroundColor(getResources().getColor(R.color.transparent));
        v_unread.setBackgroundColor(getResources().getColor(R.color.transparent));
        v_read.setBackgroundColor(getResources().getColor(R.color.transparent));

        if (id == R.id.tv_all) {
            tv_all.setTextColor(getResources().getColor(R.color.main_red));
            v_all.setBackgroundColor(getResources().getColor(R.color.main_red));
        } else if (id == R.id.tv_unread) {
            tv_unread.setTextColor(getResources().getColor(R.color.main_red));
            v_unread.setBackgroundColor(getResources().getColor(R.color.main_red));
        } else if (id == R.id.tv_read) {
            tv_read.setTextColor(getResources().getColor(R.color.main_red));
            v_read.setBackgroundColor(getResources().getColor(R.color.main_red));
        }
    }
}
